package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.logic.LogSendLogic;
	import engine.logic.event.LogicEvent;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;

	public class ChongZhiDaLiBaoLogic
	{
		private static var _self : ChongZhiDaLiBaoLogic;
		private var _ui : ChongZhiDaLiBaoUI;
		private var _model : ModelLocator;
		private var _role : RoleVo;
		private var itemArray : Array;
		protected const COLOR_TABLE	:Array	=	["<font color='#000000'>","<font color='#ffffff'>","<font color='#1eff00'>","<font color='#20BFF8'>","<font color='#a335ee'>","<font color='#E8912E'>","<font color='#e5cc80'>","<font color='#ffff00'>"];
		
		public function ChongZhiDaLiBaoLogic()
		{
		}
		
		public static function getInstance():ChongZhiDaLiBaoLogic
		{
			if(_self == null)
			{
				_self = new ChongZhiDaLiBaoLogic();
				
			}
			return _self;
		}
		
		public function get ui():ChongZhiDaLiBaoUI
		{
			if(_ui==null)
			{
				_ui = new ChongZhiDaLiBaoUI();
			}
			return _ui;
		}
		
		/**
		 * 初始化界面
		 */		
		public function Init():void
		{
			_model = ModelLocator.getInstance();
			_role = _model.scene.Role; 
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_FIRST_CHARGE_ITEM_LIST),reveiceItemList);
		}
		
		/**
		 * 显示界面
		 * 
		 */		
		public function BeforeShow():void
		{
			setLingQuBtn();
			this._ui.visible = false;
			if(_ui.backGround.source == null)
			{
				MultLoader.getInstance().load([StageInfo.swfResPath+"chongZhiDaLiBao.swf"],true,CreateUI)
			}
			else
			{
				addEvent();
			}
		}
		
		/**
		 * 关闭界面
		 * 
		 */		
		public function AfterHide():void
		{
			removeEvent();
			_ui.backGround.source = null;
		}
		
		private function CreateUI():void
		{
			this._ui.backGround.source = MultLoader.getInstance().getSourceByUrl(StageInfo.swfResPath+"chongZhiDaLiBao.swf");	
			addEvent();
		}
		
		private function addEvent():void
		{
			this._ui.visible = true;
			_ui.windowBG.visible = false;
			sendGetItemList();
			this._ui.chongZhiBtn.addEventListener(MouseEvent.CLICK,onClickChongZhiBtn);
			this._ui.lingQuBtn.addEventListener(MouseEvent.CLICK,onClickLingQuBtn);
			this._ui.ItemList.addItemsEvent(MouseEvent.MOUSE_OVER,overItemList);
			this._ui.ItemList.addItemsEvent(MouseEvent.MOUSE_OUT,outItemList);
			this._ui.closeBtn.addEventListener(MouseEvent.CLICK,closeUI);
			if(!SceneManager._itemIconLoad)
			{
				_model.addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					setItem(itemArray);
				}
			}
		}
		
		private function removeEvent():void
		{
			this._ui.chongZhiBtn.removeEventListener(MouseEvent.CLICK,onClickChongZhiBtn);
			this._ui.lingQuBtn.removeEventListener(MouseEvent.CLICK,onClickLingQuBtn);
			this._ui.ItemList.removeItemsEvent(MouseEvent.MOUSE_OVER,overItemList);
			this._ui.ItemList.removeItemsEvent(MouseEvent.MOUSE_OUT,outItemList);
			this._ui.closeBtn.removeEventListener(MouseEvent.CLICK,closeUI);
		}
		
		/**
		 *請求獎勵物品列表 
		 * 
		 */
		private function sendGetItemList():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_OPEN_FIRST_CHARGE_UI);
		}
		
		/**
		 *請求领取獎勵
		 * 
		 */
		private function sendGetPrize():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_FIRST_CHARGE_PRIZE);
		}
		
		/**
		 *關閉UI 
		 * @param event
		 * 
		 */
		private function closeUI(event : MouseEvent):void
		{
			this.ui.hide();
		}
		
		/**
		 *充值 
		 * @param event
		 * 
		 */
		private function onClickChongZhiBtn(event : MouseEvent):void
		{
			_role.recharge();
			LogSendLogic.getInstance().btnClickNumSend(22802);
		}
		
		/**
		 *领取 
		 * @param event
		 * 
		 */
		private function onClickLingQuBtn(event : MouseEvent):void
		{
			sendGetPrize();
			setLingQuBtn();
		}
		
		/**
		 *显示物品tips 
		 * @param event
		 * 
		 */
		private function overItemList(event : MouseEvent):void
		{
			var item : MovieClip = event.currentTarget as MovieClip;
			if(item.hasOwnProperty("ConfigID"))
			{
				TipsMainLogic.instance.showMain(item["ConfigID"]);
			}
		}
		
		/**
		 *影藏物品Tips 
		 * @param event
		 * 
		 */
		private function outItemList(event : MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		/**
		 *返回列表
		 * @param event
		 * 
		 */
		private function reveiceItemList(event : CustomMsgEvent):void
		{
			var p:int;
			itemArray = [];
			for(var i:int;i<4;i++)
			{
				var itemObj : Object = new Object;
				itemObj["configID"] = event.pList[p++];
				itemObj["count"] = event.pList[p++];
				itemArray.push(itemObj);
			}
			setItem(itemArray);
			setLingQuBtn();
		}
		
		private function setLingQuBtn():void
		{
			var isGetPrize : int = _model.scene.Role.GetProperties("IsGetFirstChargePrize");//是否领取过 1领取过 0没有领过
			var isCharged : int = _model.scene.Role.GetProperties("IsFirstCharged");//是否充值过  0没有  1有
			if(isGetPrize == 0 && isCharged == 1)
			{
				_ui.chongZhiBtn.visible = false;
				_ui.lingQuBtn.visible = true;
			}
			else
			{
				_ui.chongZhiBtn.visible = true;
				_ui.lingQuBtn.visible = false;
			}
		}
		
		/**
		 *设置奖品信息 
		 * @param itemArray
		 * 
		 */
		private function setItem(itemArray : Array):void
		{
			var mc : MovieClip;
			for(var i:int ; i<itemArray.length ; i++)
			{
				var imageClip : NxClip = new NxClip();
				var effectClip : NxClip = new NxClip();
				var countTxt : NxLabel = new NxLabel();
				if(i == 3)
				{
					effectClip.source = ResManager.newResClass("custSkin_17_6");
					effectClip.x = 17;
					effectClip.y = 17;
				}
				else
				{
					effectClip.source = ResManager.newResClass("custSkin_17_5");
					effectClip.x = -16;
					effectClip.y = -16;
				}
				
				countTxt.x = 29;
				countTxt.y = 22;
				mc = _ui.ItemList.getItem(0,i) as MovieClip;
				mc["ConfigID"] = itemArray[i]["configID"];
				imageClip.source = ResManager.newResClass("icon_"+StaticIni.getIniVar(itemArray[i]["configID"],"Photo")); 
				countTxt.text = itemArray[i]["count"];
				for(var j:int = 0;j<mc["img01"].numChildren;j++)
				{
					mc["img01"].removeChildAt(j);
				}
				mc["img01"].addChild(effectClip);
				mc["img01"].addChild(imageClip);
				mc["img01"].addChild(countTxt);
				if(int(countTxt.text)>1)
				{
					countTxt.visible = true;
				}
				else
				{
					countTxt.visible = false;
				}
			}
		}
	}
}